package com.dehui.traders.ui;

import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.dehui.traders.ui.msg.MsgListActivity;
import com.dehui.traders.ui.stockdetail.StocksDetailActivity;
import com.dehui.traders.ui.web.WebDetailActivity;

import org.json.JSONObject;

import cn.jpush.android.api.JPushInterface;

public class MyReceiver extends BroadcastReceiver {
    private static final String TAG = "MyReceiver";

    private NotificationManager nm;

    @Override
    public void onReceive(Context context, Intent intent) {
        if (null == nm) {
            nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        }

        Bundle bundle = intent.getExtras();
        if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
        } else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
        } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
        } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
            openNotification(context,bundle);

        } else {

        }
    }


    private void openNotification(Context context, Bundle bundle){

        String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
        Log.e("Hqq",extras);
        try {
            JSONObject extrasJson = new JSONObject(extras);

            String flag = extrasJson.optString("flag");

            if ("1".equals(flag)){
                String code = extrasJson.optString("code");
                String market = extrasJson.optString("market");
                String price = extrasJson.optString("price");
                String closePrice = extrasJson.optString("closePrice");
                String name = extrasJson.optString("name");
                String type = extrasJson.optString("type");
                StocksDetailActivity.start(context,"",name,code,closePrice,market);
            }else if ("2".equals(flag)){
                String url = extrasJson.optString("url");
                String name = extrasJson.optString("name");
                WebDetailActivity.start(context,name,url);
            }else{
                MsgListActivity.start(context);
            }

        } catch (Exception e) {
            return;
        }
    }

}
